﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ArmoryBrowser.Entities;
using System.IO;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using Silverlight.Samples;
using Jillzhang.GifUtility;
using ArmoryBrowser.Commands;
using ArmoryBrowser.Controllers;
using ArmoryBrowser.Entities.Achievements;

namespace ArmoryBrowser.ViewModels
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        private ArmoryController _controller = ArmoryController.Instance;
        private Uri serverUri = new Uri("http://armory.wow-europe.com");

        private CharacterPage _page;
        private AchievementsPage _achievementsPage;
        private DelegateCommand<String> _loadCharacterCommand;

        private String _realmName;
        private String _characterName;

        private String openItem;

        public String RealmName
        {
            get
            {
                return _realmName;
            }
            set
            {
                if (_realmName != value)
                {
                    _realmName = value;
                    OnPropertyChanged("RealmName");
                }
            }
        }

        public String CharacterName
        {
            get
            {
                return _characterName;
            }
            set
            {
                if (_characterName != value)
                {
                    _characterName = value;
                    OnPropertyChanged("CharacterName");
                }
            }
        }

        public DelegateCommand<String> LoadCharacterCommand
        {
            get
            {
                return _loadCharacterCommand;
            }
            private set
            {
                if (_loadCharacterCommand != value)
                {
                    _loadCharacterCommand = value;
                }
            }
        }

        public String OpenItem
        {
            get
            {
                return openItem;
            }
            set
            {
                if (openItem != value)
                {
                    openItem = value;
                    OnPropertyChanged("OpenItem");
                }
            }
        }

        private void OnLoadCharacter(String parm)
        {
            _controller.GetCharacterPageAsync(RealmName, CharacterName, (a =>
            {
                Page = a;

            }));

            _controller.GetAchievementsSummaryAsync(RealmName, CharacterName, (a =>
            {
                AchievementsPage = a;
            }));
        }

        public CharacterPage Page
        {
            get
            {
                return _page;
            }
            set
            {
                if (_page != value)
                {
                    _page = value;
                    OnPropertyChanged("Page");
                }
            }
        }
        public AchievementsPage AchievementsPage
        {
            get
            {
                return _achievementsPage;
            }
            set
            {
                if (_achievementsPage != value)
                {
                    _achievementsPage = value;
                    OnPropertyChanged("AchievementsPage");
                }
            }
        }

        public MainPageViewModel()
        {
            try
            {
                if (!DesignerProperties.IsInDesignTool)
                {
                    LoadCharacterCommand = new DelegateCommand<String>(OnLoadCharacter);
                }
            }
            catch
            {
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
